package Session;
import Levels.Chapter;
import Levels.ChapterId;
import Levels.Complexity;
import Questions.QuestionsBook;


/**
 * Created with IntelliJ IDEA.
 * Authorization.User: Инесса
 * Date: 25.10.13
 * Time: 1:03
 * To change this template use File | Settings | File Templates.
 */

/**
 * Этот класс выполняет функции истории для пользователя. Хранит вопросы, ответы и номер текущего вопроса.
 */
public class TestSession {
    private Chapter currentChapter;
    private QuestionsBook questionsBook;
    private static TestSession instance;
    private boolean endSession = false;
    private int currentIndex;
    private String enteredLine;
    private ChapterId currentChapterId = ChapterId.CHAPTER_ONE;

    private TestSession(){
       endSession = false;
       currentIndex = 0;
    }

    public static TestSession getInstance() {
        if(instance==null){
            instance = new TestSession();
        }
        return instance;
    }

    public void setQuestionsBook(QuestionsBook loadedBook){
        this.questionsBook=loadedBook;
        setStartingChapter(this.questionsBook);
    }



    public void save() {
        // сгенерировать теги в DOM и записать.
    }

    public void next() {
        this.currentIndex++;
    }

    public void askQuestion() {

            currentChapter.askQuestion(currentIndex);

    }

    public void prev() {
        this.currentIndex--;
    }


    public void setUserAnswer() {
        if(enteredLine!=null)
        currentChapter.setUserLevelAnswer(currentIndex,enteredLine);
        currentIndex++;
        enteredLine=null;
    }

    public boolean isEndSession() {
        return endSession;
    }

    public void setEnteredLine(String userInput){
           this.enteredLine=userInput;
    }

    private void setStartingChapter(QuestionsBook questionsBook){
        this.currentChapter = questionsBook.getChapterById(currentChapterId);
    }

    public void nextChapter(){
        this.currentChapterId=ChapterId.CHAPTER_TWO;
        this.currentChapter = questionsBook.getChapterById(currentChapterId);
        this.currentChapter.setCurrentComplexity(Complexity.EASY);
    }

    public boolean isLevelAnswered() {
        return this.currentChapter.isAnswered();
    }

    public int countAnsweredLevel() {
        return currentChapter.getLevelResult();
    }

    public void refreshChapterLevel() {
        currentChapter.shuffleChapterLevel();
    }

    public Complexity getChapterComplexity() {
        return currentChapter.getComplexity();
    }

    public void upChapterComplexity() {
        currentChapter.setCurrentComplexity(Complexity.MEDIUM);
    }


    public boolean isLastChapter() {
        return currentChapterId.equals(ChapterId.CHAPTER_TWO);
    }

    public void endSession() {
        endSession = true;
    }
}
